package com.arcpan.Service.Impl;

import cn.hutool.core.date.DateUtil;
import com.arcpan.Constant.BaseConstant;
import com.arcpan.Entity.Query.FileShareQuery;
import com.arcpan.Entity.Vo.ShareInfo;
import com.arcpan.Mapper.FileShareMapper;
import com.arcpan.Result.PageResult;
import com.arcpan.Service.FileInfoService;
import com.arcpan.Service.FileShareService;
import com.arcpan.Util.DateUtils;
import com.arcpan.Util.StringTools;
import com.arcpan.enums.ShareValidTypeEnums;
import com.github.pagehelper.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class FileShareServiceImpl  implements FileShareService {
    @Resource
    private FileShareMapper fileShareMapper;
    public PageResult getBypage(FileShareQuery fileShareQuery, Integer pageNo, Integer pageSize){
        PageResult pageResult=new PageResult();
        PageHelper.startPage(pageNo,pageSize);
        Page<ShareInfo> page=fileShareMapper.selectBypage(fileShareQuery);
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;


    }
    public   void sharefile(String userId,String fileId, Integer validType, String code){
        ShareInfo shareInfo=new ShareInfo();

        if(validType!=4){
        shareInfo.setExpireTime(DateUtils.getafter(ShareValidTypeEnums.getByType(validType).getDays()));
        }
        Date curdate=new Date();
        shareInfo.setShareTime(curdate);
        shareInfo.setCode(code);
        if(StringTools.isEmpty(code)){
            shareInfo.setCode(StringTools.getRandomNumber(BaseConstant.LENGTH_5));
        }
        shareInfo.setUserId(userId);
        shareInfo.setFileId(fileId);
        shareInfo.setShareId(StringTools.getRandomNumber(BaseConstant.LENGTH_10));
        fileShareMapper.insert(shareInfo);


    }
    public void cancel(String shareIds, String userId){
        String[] sharelist=shareIds.split(",");
       fileShareMapper.delete(sharelist,userId);

    }
}
